#include <iostream>
using namespace std;

int n, a, b, m; //获取输入
int x, y;       //拓展欧几里得结果获取

int exgcd(int a, int b, int &x, int &y)
{
    if (!b)
    {
        x = 1;
        y = 0;
        return a;
    }
    int x0, y0;
    int d = exgcd(b, a % b, x0, y0);
    x = y0;
    y = x0 - a / b * y0;
    return d;
}

int main()
{
    cin >> n;
    while (n--)
    {
        cin >> a >> b >> m;
        int d = exgcd(a, m, x, y);
        if (b % d == 0)
            cout << (long long)b / d * x % m << endl; //因为x可能比较大，b/d也可能比较大，所以可能会爆int
        else
            cout << "impossible" << endl;
    }
    return 0;
}